﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.ComponentModel;
using Skynet.BusinessEntities;
using Skynet.BusinessLogic.BusinessComponents;
using System.Xml.Linq;
using Skynet.WebFoundation.Services.Session;

namespace Skynet.WebAdministracion.Views.OrganizationMgmView
{
    [System.ComponentModel.DataObject]
    public class OrganizationMgmPresenter :  WebFoundation.Views.Master.MasterPresenter<IOrganizationMgmView>
    {
        
        [Dependency]
        public IBCCustomers BCCustomers { get; set; }
        [Dependency]
        public IBCCustomersQueries BCCustomersQueries { get; set; }

        [Dependency]
        public ISessionService SessionService { get; set; }


        [DataObjectMethod(DataObjectMethodType.Insert, false)]
        public void AddOrganization(Organization item)
        {
            item.Id = Guid.NewGuid();              
            var co = new CustomerOrganization(){ Id = Guid.NewGuid(),   IsAdmin = true, CustomerId  =  this.SessionService.CustomerId, OrganizationId = item.Id };
            item.CustomerOrganizations.Add(co);
            item.MarkAsAdded(); 
            this.BCCustomers.MaintenanceOrganization(item); 
        }

        [DataObjectMethod(DataObjectMethodType.Insert, false)]
        public void UpdateOrganization(Organization item)
        {

            item.MarkAsModified();
            this.BCCustomers.MaintenanceOrganization(item);
        }

        [DataObjectMethod(DataObjectMethodType.Insert, false)]
        public void DeleteOrganization(Organization item)
        {
            item.MarkAsDeleted();
            this.BCCustomers.MaintenanceOrganization(item);
        }



        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public Organization GetOrganization(Guid Id)
        {
            if (Id == Guid.Empty)
                return null; 
            return this.BCCustomersQueries.FindOrganization(option: FindOrganizationOptions.FindById, OrganizationId: Id);                        
        }

        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public IEnumerable<object> GetOrganizations()
        {
            IEnumerable<Object> result = null;
            var target = this.BCCustomersQueries.QueryOrganizations(QueryOrganizationOptions.GetAllByCustomerAdmin, this.SessionService.CustomerId);


            if (!string.IsNullOrEmpty(target))
            {
                var query = from c in XElement.Parse(target).Descendants("Organization")
                            select new
                            {
                                Id = Guid.Parse(c.Element("Id").Value),
                                Title = (string)c.Element("Title")                                
                            };
                result = query.ToList();
            }
            else
            {
                result = new List<object>();
            }
            return result; 
        }
    }
}
